/*
 * @Author: 黄达全
 * @Description: 工具集
 * @Date: 2024-07-19 16:29:03
 * @LastEditTime: 2024-07-21 00:14:12
 */


import type { Tool } from "@/common/type";
import sysConfig from "@/config"
import CryptoJS from 'crypto-js';


const tool: Tool = {
    // localStorage本地存储
    data: {
        set(key, data, dateTime='0') {
            // 本地存储的数据是否加密，加密方式AES
            if (sysConfig.LS_ENCRYPTION == "AES") {
                data = tool.crypto.AES.encrypt(JSON.stringify(data), sysConfig.LS_ENCRYPTION_key)
            }
            let cacheValue = {
                content: data,
                datetime: parseInt(dateTime) === 0 ? 0 : new Date().getTime() + parseInt(dateTime) * 1000
            }
            localStorage.setItem(key, JSON.stringify(cacheValue))
        },
        get(key) {
            try {
                const value = JSON.parse(localStorage.getItem(key)!)
                if (value) {
                    let nowTime = new Date().getTime()
                    if (nowTime > value.datetime && value.datetime != 0) {
                        localStorage.removeItem(key)
                        return null;
                    }
                    //解密
                    if(sysConfig.LS_ENCRYPTION == "AES"){
                        value.content = JSON.parse(tool.crypto.AES.decrypt(value.content, sysConfig.LS_ENCRYPTION_key))
                    }
                    return value.content
                }
                return null
            } catch (err) {
                return null
            }

         },
        remove(key) {
            localStorage.removeItem(key)
         },
        clear() {
            localStorage.clear()
         }
    },
    crypto: {
        //MD5加密
        MD5(data) {
            return CryptoJS.MD5(data).toString()
        },
        //BASE64加解密
        BASE64: {
            encrypt(data) {
                return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
            },
            decrypt(cipher) {
                return CryptoJS.enc.Base64.parse(cipher).toString(CryptoJS.enc.Utf8)
            }
        },
        //AES加解密
        AES: {
            encrypt(data, secretKey, config = {}) {
                if (secretKey.length % 8 != 0) {
                    console.warn("[HDQ error]: 秘钥长度需为8的倍数，否则解密将会失败。")
                }
                const result = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(secretKey), {
                    iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
                    mode: CryptoJS.mode[config.mode || "ECB"],
                    padding: CryptoJS.pad[config.padding || "Pkcs7"]
                })
                return result.toString()
            },
            decrypt(cipher, secretKey, config = {}) {
                const result = CryptoJS.AES.decrypt(cipher, CryptoJS.enc.Utf8.parse(secretKey), {
                    iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
                    mode: CryptoJS.mode[config.mode || "ECB"],
                    padding: CryptoJS.pad[config.padding || "Pkcs7"]
                })
                return CryptoJS.enc.Utf8.stringify(result);
            }
        }
    }
}

export default tool